<?php
namespace Admin\Controller;
use Think\Controller;
class CatproController extends BaseController {
    public function catpro_list(){
		$this->display();
    }
    public function catpro_ajax(){
		$limit=5;
		if(I('get.sou')){
			$cat_name=I('get.sou');
			$count=M('cat_pro')->where("cat_name like '%$cat_name%'")->count();
		}else{
			$count=M('cat_pro')->count();
		}
		$page=new \Think\Page($count,$limit);
		$show=$page->show();
		$ord=I('get.ord');
		if($ord==1){
			$order="cat_ord ASC";
		}elseif($ord==2){
			$order="cat_ord DESC";
		}else{
			$order="cat_addtime DESC,cat_id DESC";
		}
		if(I('get.sou')){
			$cat_name=I('get.sou');
			$cat_pro=M('cat_pro')
					->where("cat_name like '%$cat_name%'")
					->order($order)
					->limit($page->firstRow.','.$page->listRows)
					->select();
		}else{
			$cat_pro=M('cat_pro')
					->order($order)
					->limit($page->firstRow.','.$page->listRows)
					->select();
		}
		$this->assign('show',$show);
		$this->assign('cat_pro',$cat_pro);
		$this->assign('a',$page->firstRow+1);
		$this->display();
	}
    public function catpro_add(){
    	if(I('post.')){
			$data=array(
				'cat_name'=>I('post.cat_name'),
				'cat_addtime'=>strtotime(I('post.cat_addtime')),
				'cat_ord'=>I('post.cat_ord'),
			);
			
			$res=M('cat_pro')->add($data);
			if($res){
				$this->success('添加成功',U('Admin/Catpro/catpro_list'));
				exit();
			}else{
				$this->error('数据输入有误');
				exit();
			}
		}
		$this->display();
    }
    public function catpro_edit(){
    	$cat_id=I('get.aid');
		$cat_pro=M('cat_pro')->where("cat_id=$cat_id")->find();
		$this->assign('cat_pro',$cat_pro);
		if(I('post.')){
			$data=array(
					'cat_name'=>I('post.cat_name'),
					'cat_addtime'=>strtotime(I('post.cat_addtime')),
					'cat_ord'=>I('post.cat_ord'),
				);
			$cat_id=I('post.cat_id');
			$res=M('cat_pro')
					->where("cat_id=$cat_id")
					->save($data);
			if($res){
					$this->success('编辑成功',U('Admin/Catpro/Catpro_list'));
					exit();
				}else{
					$this->error('数据输入有误');
					exit();
				}
		}
		$this->display();
    }
    public function catpro_del(){
    	$cat_id=I('get.a_id');

    	$product=M('product')->where("cat_id=$cat_id")->select();
    	if(empty($product)){
    		$res=M('cat_pro')
    			->where("cat_id=$cat_id")
    			->delete();
    		if($res){
    			echo 1;exit();
    		}else{
    			echo M('cat_pro')->getLastSql();exit();
    		}
    	}else{
    		echo 2;exit();
    	}
    }
    public function name_ajax(){
    	// 双击修改标题
		if(!empty(I('get.value'))){
		    $id=I('get.id');
		    $value=I('get.value');
		    $data=array(
		      'cat_name'=>$value,
		    );
		    $res=M('cat_pro')
		    		->where("cat_id=$id")
		    		->save($data);
		    if($res){
		      echo 1;die;
		    }else{
		      echo 2;die;
		    }
		  }
    }
}